package com.wxg.algorithm.chapter02.basic;

import java.util.Arrays;

public class SelectionSort {

    public static void main(String[] args) {
        SelectionSort ss = new SelectionSort();
        int[] arr = {2, 1, 8, 3, 5, 4, 6, 7};

        System.out.println(Arrays.toString(arr));

        ss.sort(arr, arr.length);

        System.out.println(Arrays.toString(arr));
    }

    private void sort(int[] arr, int n) {
        for (int i = 0; i < n; i++){
            // 寻找[i, n)区间里的最小值
            int minIndex = i;
            for(int j = i + 1; j < n; j++){
                if (arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
}